using System;
using System.Collections.Generic;
using UnityEngine;

public class UISpinner : MonoBehaviour
{
	[Serializable]
	public class ImageSet
	{
		public List<UISprite> images = new List<UISprite>();

		public List<UITexture> textures = new List<UITexture>();

		public void SetSpriteName(string spriteName)
		{
			if (spriteName == null)
			{
				return;
			}
			foreach (UITexture texture in textures)
			{
				texture.gameObject.SetActive(false);
			}
			foreach (UISprite image in images)
			{
				image.gameObject.SetActive(true);
				image.spriteName = spriteName;
			}
		}

		public void SetTextures(Texture2D texture)
		{
			if (texture == null)
			{
				return;
			}
			foreach (UISprite image in images)
			{
				image.gameObject.SetActive(false);
			}
			foreach (UITexture texture2 in textures)
			{
				texture2.gameObject.SetActive(true);
				texture2.mainTexture = texture;
			}
		}

		public void SetMotionBlur(float blur)
		{
			float num = 1f;
			foreach (UISprite image in images)
			{
				image.alpha = num * blur;
				num *= 0.5f;
			}
			images[0].alpha = 1f;
			num = 1f;
			foreach (UITexture texture in textures)
			{
				texture.alpha = num * blur;
				num *= 0.5f;
			}
			textures[0].alpha = 1f;
		}
	}

	public struct State
	{
		public bool spinning;

		public int avatarIndex;

		public bool avatarChosen;

		public int finalAvatar;

		public bool finalSpin;

		public float time;
	}

	public List<ImageSet> avatars = new List<ImageSet>();

	public Transform avatarsTable;

	public float changeDuration;

	public float stoppingDuration = 0.5f;

	protected State state;

	protected float imageHeight;

	private GGServerRequestsBackend.TextureRequest textureRequest;

	private Texture2D endTexture;

	private void Start()
	{
		UISprite uISprite = avatars[0].images[0];
		Vector3[] localCorners = uISprite.localCorners;
		Vector3 vector = localCorners[0];
		Vector3 vector2 = localCorners[1];
		imageHeight = vector2.y - vector.y;
		ChangeAvatar(state.avatarIndex, state.avatarIndex + 1);
	}

	public void ChangeAvatar(int firstAvatarIndex, int secondAvatarIndex)
	{
		int num = Enum.GetNames(typeof(GameConstants.Flags)).Length;
		GameConstants instance = GameConstants.Instance;
		firstAvatarIndex %= num;
		secondAvatarIndex %= num;
		GameConstants.Country country = instance.CountryForFlag(firstAvatarIndex);
		GameConstants.Country country2 = instance.CountryForFlag(secondAvatarIndex);
		if (country != null)
		{
			avatars[0].SetSpriteName(country.spriteName);
		}
		if (country2 != null)
		{
			avatars[1].SetSpriteName(country2.spriteName);
		}
	}

	public void StartSpinning()
	{
		state = default(State);
		state.spinning = true;
		avatarsTable.position = Vector3.zero;
		foreach (ImageSet avatar in avatars)
		{
			avatar.SetMotionBlur(1f);
		}
	}

	public void StopSpinWithSprite(int flag, string finalTexture = "")
	{
		state.avatarChosen = true;
		state.finalAvatar = flag;
		if (GGPlayerSettings.instance.useProfilePictureInMultiplayer && finalTexture != string.Empty)
		{
			textureRequest = new GGServerRequestsBackend.TextureRequest(finalTexture);
			textureRequest.cache = CacheStategy.GetFromCache;
			textureRequest.onComplete = OnRequestComplete;
			BehaviourSingleton<GGRequestLimitter>.instance.Add(textureRequest);
		}
	}

	public void OnRequestComplete(GGServerRequestsBackend.ServerRequest request)
	{
		GGServerRequestsBackend.TextureRequest textureRequest = request as GGServerRequestsBackend.TextureRequest;
		if (textureRequest == null || textureRequest != this.textureRequest)
		{
			return;
		}
		if (request.status != 0)
		{
			this.textureRequest = null;
			return;
		}
		endTexture = textureRequest.response;
		avatars[1].SetTextures(endTexture);
		if (!state.spinning)
		{
			avatars[0].SetTextures(endTexture);
		}
	}

	private void Update()
	{
		if (!state.spinning)
		{
			return;
		}
		state.time += Time.deltaTime;
		Interpolate.Function function = Interpolate.Ease(Interpolate.EaseType.Linear);
		float num = ((!state.finalSpin) ? changeDuration : stoppingDuration);
		if (state.time >= num)
		{
			state.avatarIndex++;
			state.time = 0f;
			if (state.finalSpin)
			{
				if (endTexture != null)
				{
					avatars[0].SetTextures(endTexture);
					avatars[1].SetTextures(endTexture);
					endTexture = null;
				}
				else
				{
					ChangeAvatar(state.finalAvatar, state.finalAvatar);
				}
				state.spinning = false;
			}
			else if (state.avatarChosen)
			{
				ChangeAvatar(state.avatarIndex, state.finalAvatar);
				state.finalSpin = true;
				foreach (ImageSet avatar in avatars)
				{
					avatar.SetMotionBlur(0f);
				}
			}
			else
			{
				ChangeAvatar(state.avatarIndex, state.avatarIndex + 1);
			}
		}
		Interpolate.EaseType type = (state.finalSpin ? Interpolate.EaseType.EaseOutQuad : Interpolate.EaseType.Linear);
		Vector3 localPosition = Interpolate.Ease(Interpolate.Ease(type), Vector3.zero, new Vector3(0f, 0f - imageHeight, 0f), state.time, num);
		avatarsTable.transform.localPosition = localPosition;
	}
}
